<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 添加分类 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="addopt">添加分类</el-button>
        </el-col>
      </el-row>
      <table-tree class="terr" :data='CataList' :columns='columns' :selection-type='false' :expand-type='false' show-index border>
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scoped">
          <i style="color:lightgreen" v-if="!scoped.row.cat_deleted" class="el-icon-success"></i>
          <i style="color:red" v-if="scoped.row.cat_deleted" class="el-icon-error"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scoped">
          <el-tag v-if="scoped.row.cat_level ===0">一级</el-tag>
          <el-tag v-if="scoped.row.cat_level ===1" type="success">二级</el-tag>
          <el-tag v-if="scoped.row.cat_level ===2" type="warning">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="scoped">
          <!-- 编辑分类 -->
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="editorCate(scoped.row)">编辑</el-button>
          <!-- 删除分类 -->
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="delCate(scoped.row)">删除</el-button>
        </template>
      </table-tree>
      <!-- 分页功能 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="querInfo.pagenum" :page-sizes="[3, 5, 7, 9]" :page-size="querInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!-- 添加分类的对话框 -->

    <el-dialog title="添加分类" :visible.sync="dialogVisible" width="30%" @close="addCatedialog">
      <!-- 添加分类的主体 -->
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="ruleForm.cat_name" placeholder="请输入分类名称"></el-input>
        </el-form-item>
        <!-- 父级分类 -->
        <el-form-item label="父级分类：">
          <el-cascader v-model="value" :options="parentCateList" :props="props" clearable @change="parent"></el-cascader>
        </el-form-item>
      </el-form>
      <!-- 添加分类的底部 -->
      <span slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑分类的对话框 -->
    <el-dialog title="编辑分类" :visible.sync="editorCateVisible" width="30%">
      <el-form :model="editorCateValue" :rules="rules" ref="editorCateValue" label-width="100px" class="demo-ruleForm">
        <el-form-item label="分类名称"  prop="name">
          <el-input v-model="editorCateValue.name" placeholder="请输入修改的名称"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editorCateVisible = false">取 消</el-button>
        <el-button type="primary" @click="editor">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 商品分类数据列表
      CataList: [],
      // 商品分类的页码
      querInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 商品总数
      total: 0,
      // 显示隐藏添加分类的对话框
      dialogVisible: false,
      // 添加分类的对象
      ruleForm: {
        cat_name: '',
        cat_level: 0,
        cat_pid: 0
      },
      // 分类表格数据
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        { label: '排序', type: 'template', template: 'order' },
        { label: '操作', type: 'template', template: 'opt' }
      ],
      rules: {
        cat_name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      },
      // 接收父级分类的数据
      parentCateList: [],
      // 接收添加父级分类的数据
      value: [],
      // 指定联级选择器的数据列表
      props: {
        parentCateList: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true
      },
      // 添加分类的对象
      addCateForm: {
        cat_level: 0
      },
      // 编辑分类对象的数据
      editorCateValue: { name: '' },
      // 显示隐藏编辑分类
      editorCateVisible: false,
      // 获取编辑分类的id
      scp: {}
    }
  },
  methods: {
    async getCateList () {
      const { data: res } = await this.$http.get('categories', {
        params: this.querInfo
      })
      if (res.meta.status !== 200) return this.$message.error('获取列表失败')
      this.CataList = res.data.result
      this.total = res.data.total
      console.log(this.total)
    },
    // 分页
    handleSizeChange (newpage) {
      this.querInfo.pagesize = newpage
      this.getCateList()
    },
    // 分页
    handleCurrentChange (newpage) {
      this.querInfo.pagenum = newpage
      this.getCateList()
    },
    // 添加分类
    addopt () {
      this.dialogVisible = true
      this.getParentCateList()
    },
    // 获取父级分类数据
    async getParentCateList () {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) this.$message.error('获取失败')
      this.parentCateList = res.data
      console.log(this.parentCateList)
    },
    parent () {
      console.log(this.value)
      if (this.value.length > 0) {
        // 父级分类的id
        this.ruleForm.cat_pid = this.value[this.value.length - 1]
        // 为当前分类的等级赋值
        this.ruleForm.cat_level = this.value.length
        return ''
      } else {
        // 父级分类的id
        this.ruleForm.cat_pid = 0
        // 为当前分类的等级赋值
        this.ruleForm.cat_level = 0
      }
    },
    // 添加分类的按钮
    addCate () {
      this.$refs.ruleForm.validate(async (val) => {
        if (val) {
          console.log(this.ruleForm)
          const { data: res } = await this.$http.post(
            'categories',
            this.ruleForm
          )
          console.log(res)
          if (res.meta.status !== 201) return this.$message.error('创建失败')
          this.$message.success('创建成功')
          this.dialogVisible = false
          this.getCateList()
        }
      })
    },
    // 添加分类的选项
    addCatedialog () {
      this.$refs.ruleForm.resetFields()
      this.value = []
      this.ruleForm.cat_level = 0
      this.ruleForm.cat_pid = 0
    },
    // 删除分类的选项
    async delCate (scoped) {
      const { data: res } = await this.$http.delete(
        `categories/${scoped.cat_id}`
      )
      scoped = res
      this.getCateList()
    },
    // 编辑分类
    editorCate (scoped) {
      this.scp = scoped
      this.editorCateVisible = true
    },
    async editor () {
      const { data: res } = await this.$http.put(
        `categories/${this.scp.cat_id}`,
        {
          cat_name: this.editorCateValue.name
        }
      )
      console.log(res)
      console.log(this.editorCateValue.name)
    }
  },
  mounted () {
    this.getCateList()
  }
}
</script>

<style >
.zk-table__body-row {
  font-size: 16px;
}
.zk-table__cell-inner {
  font-size: 16px;
  width: 100%;
}
.terr {
  margin-top: 10px;
}
.el-cascader {
  width: 100%;
}
</style>
